package com.xunqi.ad.service.impl;

import com.xunqi.ad.entity.AdUser;
import com.xunqi.ad.entity.Creative;
import com.xunqi.ad.enums.ResponseEnum;
import com.xunqi.ad.form.CreativeForm;
import com.xunqi.ad.repository.AdUserRepository;
import com.xunqi.ad.repository.CreativeRepository;
import com.xunqi.ad.service.ICreativeService;
import com.xunqi.ad.vo.CreativeResponseVo;
import com.xunqi.ad.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-04-30 21:01
 **/
@Service("creativeService")
public class CreativeServiceImpl implements ICreativeService {

    @Resource
    private CreativeRepository creativeRepository;

    @Resource
    private AdUserRepository adUserRepository;


    /**
     * 创建对象信息
     * @param form
     * @return
     */
    @Override
    public ResponseVo<CreativeResponseVo> createCreative(CreativeForm form) {

        //查询用户是否存在
        Optional<AdUser> user = adUserRepository.findById(form.getUserId());
        if (!user.isPresent()) {
            return ResponseVo.error(ResponseEnum.CAN_NOT_FIND_RECORD);
        }

        //添加对象信息
        Creative creative = creativeRepository.save(form.convertToEntity());

        CreativeResponseVo creativeResponseVo = new CreativeResponseVo();
        BeanUtils.copyProperties(creative,creativeResponseVo);

        return ResponseVo.success(creativeResponseVo);
    }
}
